import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
// import BlogList from '../components/blog/List.vue'
const BlogList = () => import(/* webpackChunkName: "blog" */ '../components/blog/List.vue')
// import Add from '../components/blog/Add.vue'
const Add = () => import(/* webpackChunkName: "blog" */ '../components/blog/Add.vue')
// import MicList from '../components/blog/MicList.vue'
const MicList = () => import(/* webpackChunkName: "blog" */ '../components/blog/MicList.vue')
// import PicList from '../components/blog/PicList.vue'
const PicList = () => import(/* webpackChunkName: "blog" */ '../components/blog/PicList.vue')
// import LoginLog from '../components/admin/LoginLog.vue'
const LoginLog = () => import(/* webpackChunkName: "admin" */ '../components/admin/LoginLog.vue')
// import Admin from '../components/admin/Admin.vue'
const Admin = () => import(/* webpackChunkName: "admin" */ '../components/admin/Admin.vue')
// import Operate from '../components/admin/Operate.vue'
const Operate = () => import(/* webpackChunkName: "admin" */ '../components/admin/Operate.vue')
// import Users from '../components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "user" */ '../components/user/Users.vue')
// import Count from '../components/count/Count.vue'
const Count = () => import(/* webpackChunkName: "count" */ '../components/count/Count.vue')
// import Count from '../components/count/Email.vue'
const Email = () => import(/* webpackChunkName: "count" */ '../components/count/Email.vue')
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "home" */ '../components/Home.vue')
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "login" */ '../components/Login.vue')
const Rule = () => import(/* webpackChunkName: "login" */ '../components/admin/Rules.vue')
const NotFound = () => import(/* webpackChunkName: "login" */ '../components/NotFound.vue')
Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  name: 'login',
  component: Login
}, {
  path: '/home',
  name: 'home',
  component: Home,
  redirect: '/count',
  children: [{
    name: 'count',
    path: '/count',
    component: Count,
    meta: {
      authName: '登岛数据'
    }
  }, {
    name: 'email',
    path: '/email',
    component: Email,
    meta: {
      authName: '岛民信箱'
    }
  }, {
    name: 'users',
    path: '/users',
    component: Users,
    meta: {
      authName: '游客列表'
    }
  }, {
    name: 'loginlog',
    path: '/loginlog',
    component: LoginLog,
    meta: {
      authName: '登录信息'
    }
  }, {
    name: 'rule',
    path: '/rule',
    component: Rule,
    meta: {
      authName: '用户准则'
    }
  }, {
    name: 'adminedit',
    path: '/adminedit',
    component: Admin,
    meta: {
      authName: '音频列表'
    }

  }, {
    name: 'Operate',
    path: '/Operate',
    component: Operate,
    meta: {
      authName: '操作信息'
    }
  }, {
    name: 'bloglist',
    path: '/bloglist',
    component: BlogList,
    meta: {
      authName: '博文列表'
    }

  }, {
    name: 'add',
    path: '/blog/add',
    component: Add,
    meta: {
      authName: '编辑博文'
    }
  }, {
    name: 'piclist',
    path: '/piclist',
    component: PicList,
    meta: {
      authName: '图片列表'
    }

  }, {
    name: 'miclist',
    path: '/miclist',
    component: MicList,
    meta: {
      authName: '音频列表'
    }
  }]
}, {
  path: '*',
  component: NotFound
}
]

const router = new VueRouter({
  mode: 'history',
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.localStorage.getItem('access_token')
  // 没有token， 强制转到login页面
  if (!tokenStr) return next('/login')
  next()
  store.dispatch('worktabRoute', {
    to: {
      name: to.name ? to.name : '',
      authName: (to.meta && to.meta.authName) ? to.meta.authName : '',
      path: to.path
    },
    from: {
      name: from.name ? from.name : '',
      authName: (from.meta && from.meta.authName) ? from.meta.authName : '',
      path: from.path
    }
  })
})
export default router
